package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "9021000129647610";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key ="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCG6KxU5LPsxvGiCN6R/GalvFIfXVArkR0h4kK+rCGt0yzjnfdkATbqBJwrcjPxHfG/Ept34eZ6skm+uDU4DOdt9MmcbKpOd/Z7y60yZTuMNMjt/4naUbSWySGUjoqGXmYK7yu4DMnGleje0xm7VaL6W1T9CXktvjCJjZ9I0ZpikYes15AKnfRscoOeb6xLL4tq6Ys3lU9QIl4XwEiNkkYSlYuVSsw5Qmmld3SWiTnM2G8+T3ZGHx+JEJdvGQhn3gpvK3Y2ggWtXarK55TVa7Qlw4dLAhrJhvojK4U9Ceh88yP6dubQWCHUAMhyrvYgpkRW3dTOAZulkUa4m7EY6+KlAgMBAAECggEAFxR/RxbJYVy3wgzaZwdTWE9bjVodeRaecUmQOaahLGfQajCAPm+mwWqAfT5QfJOu5A1+44eWMq+Fxkclc61heoJCa6x7hmknlg5GVzTSdy4gofbx0pPf3VwTjqLEODC2LHC2fgpSCtNiv5xF7rK76zDs3U1dmeHL0/ySn5acXvhfE1os9DRNNZq3TM1M+c/8biVhKXJvgiz/8A+2gA5KQiYfg0tSQpjDN5Z0EeseyL/aF1FC1c9eyGpEor+hA+tcQyCJUGm0cwMMG/+3thDWKYlYq0TCDG5XCbO1Z6OfY/R27V7PDCabPkTu5n5mmqf6r7r+Z9EBPDuGBA2BczJtgQKBgQDF21uLvEzJceq1IB/SQwLbV+GyuykgKI7yAKMq89qXL7flxkUakacrmWfKfWV+jnMbN/Ujq5YHVbikwehqGRLiPXi77K7Fpo72NJkGug6LxYp0e9r2CGt2ObOqeGQI1REGThnCZ/xF5QZZUPop0pgNLscKFKKEEdPse+8uv3g9LQKBgQCujcehtKuNkoGqs+jKQIDvUV5Co5zXdpC5snmqntBhhOC3Ca7k8FP2hvRF5mDQzP6ELNFeaBITuvIXGCHUBUOG2OgOBFhMnSY4Zi3055EepABl5/ZXx/LLPYVkU71eKJJRgdr8hRcZ1fMZ14QX/nKOqHYvCEtulkeTa3I0s8fWWQKBgCaNgZ8Ox9+QxO0aH1k3GX1n+GB9O5w6iu/409Jwxa0j7S9mnNx6eRpzv9ei633nf+JT2v5iVs+DEoF4DzNtQ1K2K6piigOVWtH6mb7HzkHJ8hqUb6v1JAtbXvjekI9wBOs3mKla3bs4zy3kK3quwFYBQWNjde9KEgNIeuifAmzZAoGBAIEdlMZTWvZ8oXeAXriEYBsl6ACI/7INHN6PWxnPIbqQzJv4CKuF7cWUa8nssD6i870XXcNkXcLEbOCSVCt9vUZY2cXMhvgc4jGju2jFhX3RuA2pOHoUB884cZwVqSSmBB1QXYiedckBjjHWTfpll51YlRsTGqDxfn5YYwEC3tX5AoGAJtqv/b/AoOBgHrXUOuRPxyCZVJm/LTUnO534mzVCl6IIJGQGpRLUqx31CEehWiubl9HI0DNQOagrXMhE+hjHxA/lYjPGHojrKMMz+GvVfEyQwvAEBCVEB6zf22+tydmbM2QdOFDT4ZsUiewCd3w+iVp1/BQuBsoN1rU6MlsGsD0=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmojqrOcHy7xQ5zY9tnI0XAz2WkaKX/kSO7gBKu5leXqKlHVQXb3/kvvnflGSeHcReSw+yho0I8yAkaW6ypE+sF/lHzx0nB6K9WUDjsJ7tv4+UHG1nB7dgR83RZOrvWnk9ipsEZ/2LCG08ZuwXX1Ytpfme4EfdKjqjE54/ADPkExpJXHmYgD/Vk0pIDXUFL1CY9Iy1JrXXPjUZkMVyG39pM25pehqblpcEFpWk319Lt0DOmMD+zPJhNhOXpBiSYHCqwip7nsNQVoL+RNI2MaQsIqS6Qy7ibE+w6cSnXO3gXNtrsbBklktcIw/fCYmcqOKlZPTtibL6qpy8fuocHCQ4wIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://c3c36v.natappfree.cc/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout="30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
